/*
 * MIT License
 *
 * Copyright (c) 2023-present, tangli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package tony.crypto.asymmetric

import java.security.KeyPair
import tony.crypto.CryptoAlgorithm

/**
 * 非对称 加密/解密/签名/验签
 *
 * @author tangli
 * @date 2025/01/22 20:00
 */
public sealed interface AsymmetricCrypto : CryptoAlgorithm {
    /**
     * 生成密钥对
     *
     * @return [KeyPair] 密钥对，包含公钥和私钥
     * @throws Exception 密钥生成失败时抛出异常
     * @author tangli
     * @date 2025/01/22 20:00
     */
    public fun generateKeyPair(): KeyPair

    /**
     * 加密数据
     *
     * 使用公钥对明文数据进行加密
     *
     * @param [src] 待加密的明文数据
     * @param [secret] 公钥的字节数组表示
     * @return [ByteArray] 加密后的密文数据
     * @throws Exception 加密失败时抛出异常
     * @author tangli
     * @date 2025/01/22 20:00
     */
    override fun encrypt(
        src: ByteArray,
        secret: ByteArray,
    ): ByteArray

    /**
     * 解密数据
     *
     * 使用私钥对密文数据进行解密
     *
     * @param [src] 待解密的密文数据
     * @param [secret] 私钥的字节数组表示
     * @return [ByteArray] 解密后的明文数据
     * @throws Exception 解密失败时抛出异常
     * @author tangli
     * @date 2025/01/22 20:00
     */
    override fun decrypt(
        src: ByteArray,
        secret: ByteArray,
    ): ByteArray

    /**
     * 数字签名
     *
     * 使用私钥对数据进行数字签名
     *
     * @param [src] 待签名的原始数据
     * @param [privateKey] 私钥的字节数组表示
     * @return [ByteArray] 数字签名结果
     * @throws Exception 签名失败时抛出异常
     * @author tangli
     * @date 2025/01/22 20:00
     */
    public fun sign(
        src: ByteArray,
        privateKey: ByteArray,
    ): ByteArray

    /**
     * 验证签名
     *
     * 使用公钥验证数字签名的有效性
     *
     * @param [src] 原始数据
     * @param [signature] 数字签名数据
     * @param [publicKey] 公钥的字节数组表示
     * @return [Boolean] 验签结果，true表示验签成功，false表示验签失败
     * @author tangli
     * @date 2025/01/22 20:00
     */
    public fun verify(
        src: ByteArray,
        signature: ByteArray,
        publicKey: ByteArray,
    ): Boolean
}
